# RUN: llc -emit-call-site-info -run-pass=livedebugvalues -march=x86-64 -o - %s | FileCheck %s

## Compiled from source:
## __attribute__((noinline))
## int foo (int x, int y, int a) {
##   int i;
##   for (i = 0; i < x * y; i++) {
##     if (i < x) {
##       a = a * x;
##       break;
##    }
##   }
##   return a;
## }
## Using commands:
## $ clang -g -O1 test.c -emit-llvm -S -o test.ll
## $ llc -emit-call-site-info -stop-after stackmap-liveness test.ll -o test.mir

## Artificially added DBG_VALUE $esi, $noreg, !14, !DIExpression() into
## for.cond Basic Block to prevent further usage of Entry Value in the loop.

# CHECK: ![[ARG_Y:.*]] = !DILocalVariable(name: "y"
# CHECK: for.cond:
# CHECK: DBG_VALUE $esi, $noreg, ![[ARG_Y]], !DIExpression()
# CHECK-NOT: DBG_VALUE $esi, $noreg, ![[ARG_Y]], !DIExpression(DW_OP_LLVM_entry_value, 1)
--- |
  ; ModuleID = 'test.ll'
  source_filename = "test.c"
  target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
  target triple = "x86_64-unknown-linux-gnu"
  
  ; Function Attrs: nofree noinline norecurse nosync nounwind readnone uwtable
  define dso_local i32 @foo(i32 %x, i32 %y, i32 %a) local_unnamed_addr !dbg !8 {
  entry:
    call void @llvm.dbg.value(metadata i32 %x, metadata !13, metadata !DIExpression()), !dbg !17
    call void @llvm.dbg.value(metadata i32 %y, metadata !14, metadata !DIExpression()), !dbg !17
    call void @llvm.dbg.value(metadata i32 %a, metadata !15, metadata !DIExpression()), !dbg !17
    call void @llvm.dbg.value(metadata i32 0, metadata !16, metadata !DIExpression()), !dbg !17
    %mul = mul nsw i32 %y, %x
    call void @llvm.dbg.value(metadata i32 0, metadata !16, metadata !DIExpression()), !dbg !17
    %cmp9 = icmp sgt i32 %mul, 0, !dbg !18
    br i1 %cmp9, label %for.body.preheader, label %for.end, !dbg !21
  
  for.body.preheader:                               ; preds = %entry
    %0 = mul i32 %y, %x, !dbg !21
    br label %for.body, !dbg !21
  
  for.cond:                                         ; preds = %for.body
    call void @llvm.dbg.value(metadata i32 undef, metadata !16, metadata !DIExpression()), !dbg !17
    %lsr.iv.next = add i32 %lsr.iv, -1, !dbg !18
    %exitcond.not = icmp eq i32 %lsr.iv.next, 0, !dbg !18
    br i1 %exitcond.not, label %for.end, label %for.body, !dbg !21, !llvm.loop !22
  
  for.body:                                         ; preds = %for.cond, %for.body.preheader
    %lsr.iv = phi i32 [ %lsr.iv.next, %for.cond ], [ %0, %for.body.preheader ]
    %1 = icmp sgt i32 %x, 0
    call void @llvm.dbg.value(metadata i32 undef, metadata !16, metadata !DIExpression()), !dbg !17
    call void @llvm.dbg.value(metadata i32 undef, metadata !16, metadata !DIExpression()), !dbg !17
    br i1 %1, label %if.then, label %for.cond, !dbg !26
  
  if.then:                                          ; preds = %for.body
    %mul2 = mul nsw i32 %a, %x, !dbg !28
    call void @llvm.dbg.value(metadata i32 %mul2, metadata !15, metadata !DIExpression()), !dbg !17
    br label %for.end, !dbg !31
  
  for.end:                                          ; preds = %for.cond, %if.then, %entry
    %a.addr.0 = phi i32 [ %mul2, %if.then ], [ %a, %entry ], [ %a, %for.cond ]
    call void @llvm.dbg.value(metadata i32 %a.addr.0, metadata !15, metadata !DIExpression()), !dbg !17
    ret i32 %a.addr.0, !dbg !32
  }
  
  ; Function Attrs: nofree nosync nounwind readnone speculatable willreturn
  declare void @llvm.dbg.value(metadata, metadata, metadata)

  !llvm.dbg.cu = !{!0}
  !llvm.module.flags = !{!3, !4, !5, !6}
  !llvm.ident = !{!7}
  
  !0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 13.0.0", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, splitDebugInlining: false, nameTableKind: None)
  !1 = !DIFile(filename: "test.c", directory: "/dir")
  !2 = !{}
  !3 = !{i32 7, !"Dwarf Version", i32 4}
  !4 = !{i32 2, !"Debug Info Version", i32 3}
  !5 = !{i32 1, !"wchar_size", i32 4}
  !6 = !{i32 7, !"uwtable", i32 1}
  !7 = !{!"clang version 13.0.0"}
  !8 = distinct !DISubprogram(name: "foo", scope: !1, file: !1, line: 2, type: !9, scopeLine: 2, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !12)
  !9 = !DISubroutineType(types: !10)
  !10 = !{!11, !11, !11, !11}
  !11 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
  !12 = !{!13, !14, !15, !16}
  !13 = !DILocalVariable(name: "x", arg: 1, scope: !8, file: !1, line: 2, type: !11)
  !14 = !DILocalVariable(name: "y", arg: 2, scope: !8, file: !1, line: 2, type: !11)
  !15 = !DILocalVariable(name: "a", arg: 3, scope: !8, file: !1, line: 2, type: !11)
  !16 = !DILocalVariable(name: "i", scope: !8, file: !1, line: 3, type: !11)
  !17 = !DILocation(line: 0, scope: !8)
  !18 = !DILocation(line: 4, column: 17, scope: !19)
  !19 = distinct !DILexicalBlock(scope: !20, file: !1, line: 4, column: 3)
  !20 = distinct !DILexicalBlock(scope: !8, file: !1, line: 4, column: 3)
  !21 = !DILocation(line: 4, column: 3, scope: !20)
  !22 = distinct !{!22, !21, !23, !24, !25}
  !23 = !DILocation(line: 9, column: 3, scope: !20)
  !24 = !{!"llvm.loop.mustprogress"}
  !25 = !{!"llvm.loop.unroll.disable"}
  !26 = !DILocation(line: 5, column: 9, scope: !27)
  !27 = distinct !DILexicalBlock(scope: !19, file: !1, line: 4, column: 31)
  !28 = !DILocation(line: 6, column: 13, scope: !29)
  !29 = distinct !DILexicalBlock(scope: !30, file: !1, line: 5, column: 16)
  !30 = distinct !DILexicalBlock(scope: !27, file: !1, line: 5, column: 9)
  !31 = !DILocation(line: 7, column: 7, scope: !29)
  !32 = !DILocation(line: 10, column: 3, scope: !8)

...
---
name:            foo
alignment:       16
liveins:
  - { reg: '$edi', virtual-reg: '' }
  - { reg: '$esi', virtual-reg: '' }
  - { reg: '$edx', virtual-reg: '' }
body:             |
  bb.0.entry:
    successors: %bb.2(0x50000000), %bb.4(0x30000000)
    liveins: $edi, $edx, $esi
  
    DBG_VALUE $edi, $noreg, !13, !DIExpression(), debug-location !17
    DBG_VALUE $esi, $noreg, !14, !DIExpression(), debug-location !17
    DBG_VALUE $edx, $noreg, !15, !DIExpression(), debug-location !17
    $eax = MOV32rr $edx
    DBG_VALUE 0, $noreg, !16, !DIExpression(), debug-location !17
    DBG_VALUE $eax, $noreg, !15, !DIExpression(), debug-location !17
    renamable $esi = nsw IMUL32rr killed renamable $esi, renamable $edi, implicit-def dead $eflags
    TEST32rr renamable $esi, renamable $esi, implicit-def $eflags, debug-location !18
    JCC_1 %bb.4, 14, implicit $eflags, debug-location !21
  
  bb.2.for.body (align 16):
    successors: %bb.3(0x04000000), %bb.1(0x7c000000)
    liveins: $eax, $edi, $esi
  
    TEST32rr renamable $edi, renamable $edi, implicit-def $eflags
    DBG_VALUE $noreg, $noreg, !16, !DIExpression(), debug-location !17
    JCC_1 %bb.3, 15, implicit $eflags, debug-location !26
  
  bb.1.for.cond:
    successors: %bb.4(0x04000000), %bb.2(0x7c000000)
    liveins: $eax, $edi, $esi
  
    DBG_VALUE $esi, $noreg, !14, !DIExpression(), debug-location !17
    DBG_VALUE $noreg, $noreg, !16, !DIExpression(), debug-location !17
    renamable $esi = ADD32ri8 killed renamable $esi, -1, implicit-def $eflags, debug-location !18
    JCC_1 %bb.2, 5, implicit $eflags, debug-location !21
  
  bb.4.for.end:
    liveins: $eax
  
    DBG_VALUE $eax, $noreg, !15, !DIExpression(), debug-location !17
    RET64 $eax, debug-location !32
  
  bb.3.if.then:
    liveins: $eax, $edi
  
    renamable $eax = nsw IMUL32rr killed renamable $eax, killed renamable $edi, implicit-def dead $eflags, debug-location !28
    DBG_VALUE $eax, $noreg, !15, !DIExpression(), debug-location !17
    DBG_VALUE $eax, $noreg, !15, !DIExpression(), debug-location !17
    RET64 $eax, debug-location !32

...
